package com.tssup.fesco.service.impl;

import com.tssup.fesco.sales.goods.persistent.mapper.LSalesVolumeMapper;
import com.tssup.fesco.sales.goods.persistent.mapper.RBrandMMapper;
import com.tssup.fesco.sales.goods.persistent.mapper.RGoodsMapper;
import com.tssup.fesco.sales.goods.persistent.mapper.RGoodsSpecificationRelaMapper;
import com.tssup.fesco.sales.goods.persistent.mapper.RSalesCatalogMapper;
import com.tssup.fesco.sales.goods.persistent.mapper.RSpuDescMMapper;
import com.tssup.fesco.sales.goods.persistent.mapper.RSpuMMapper;
import com.tssup.fesco.sales.goods.persistent.pojo.RBrandM;
import com.tssup.fesco.sales.goods.persistent.pojo.RGoods;
import com.tssup.fesco.sales.goods.persistent.pojo.RGoodsDesc;
import com.tssup.fesco.sales.goods.persistent.pojo.RSalesCatalog;
import com.tssup.fesco.sales.goods.persistent.pojo.RSalesPrice;
import com.tssup.fesco.sales.goods.persistent.pojo.RSpuDescM;
import com.tssup.fesco.sales.goods.persistent.pojo.RSpuM;
import com.tssup.fesco.sales.goods.persistent.pojo.SpecificationParam;
import com.tssup.fesco.search.dao.RGoodsDescDao;
import com.tssup.fesco.search.dao.RSalesPriceDao;
import com.tssup.fesco.search.dao.RSalesStatusDao;
import com.tssup.fesco.sales.goods.persistent.pojo.RSalesStatus;
import com.tssup.fesco.search.utils.SearchUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

import com.alibaba.fastjson.JSON;

/**
 * @author jiashen
 */
@Service
public class GoodsInfoService {
    @Autowired
    private RSalesPriceDao rSalesPriceDao;

    @Autowired
    private RGoodsDescDao rGoodsDescDao;

    @Autowired
    private RSalesStatusDao rSalesStatusDao;

    @Autowired
    private RSalesCatalogMapper salesCatalogMapper;

    @Autowired
    private RSpuMMapper spuMMapper;

    @Autowired
    private RGoodsMapper goodsMapper;

    @Autowired
    private RBrandMMapper brandMMapper;

    @Autowired
    private RGoodsSpecificationRelaMapper goodsSpecificationRelaMapper;

    @Autowired
    private LSalesVolumeMapper salesVolumeMapper;

    @Autowired
    private RSpuDescMMapper spuDescMMapper;

    public HashMap<String, Object> getGoodsInfo(Integer skuId) {
        RSalesPrice rSalesPrice = rSalesPriceDao.getRSalesPriceBySkuId(skuId);
        RGoodsDesc goodsDesc = rGoodsDescDao.getRGoodsDescBySkuId(skuId);
        RGoods goods = goodsMapper.selectByPrimaryKey(skuId);

        Integer brandId = 0;
        String brandName = "";
        String brandNameChn = "";
        String brandNameChnFirstChar = "";
        Integer brandStatus = 0;

        Integer secondCatalogId = 0;
        String secondCatalogName = "";
        Integer secondCatalogIsShow = 0;
        Integer thirdCatalogId = 0;
        String thirdCatalogName = "";
        Integer thirdCatalogParentId = 0;
        Integer thirdCatalogIsShow = 0;

        if (goods.getSpu() != null) {
            RSpuM spuM = spuMMapper.selectByPrimaryKey(goods.getSpu());
            if (spuM != null) {
                // 分类信息
                thirdCatalogId = spuM.getCatalogId();
                RSalesCatalog thirdCatalog = salesCatalogMapper.selectByPrimaryKey(thirdCatalogId);
                if (thirdCatalog != null) {
                    thirdCatalogName = thirdCatalog.getCatalogName();
                    thirdCatalogIsShow = thirdCatalog.getIsShow();
                    thirdCatalogParentId = thirdCatalog.getParentId();

                    RSalesCatalog secondCatalog = salesCatalogMapper.selectByPrimaryKey(thirdCatalogParentId);
                    if (secondCatalog != null) {
                        secondCatalogId = secondCatalog.getCatalogId();
                        secondCatalogName = secondCatalog.getCatalogName();
                        secondCatalogIsShow = secondCatalog.getIsShow();
                    }
                }

                // 品牌信息
                brandId = spuM.getBrandId();
                RBrandM brandM = brandMMapper.selectByPrimaryKey(brandId);
                if (brandM != null) {
                    brandName = brandM.getBrandName();
                    brandNameChn = brandM.getChn();
                    brandStatus = brandM.getStatus();
                    if (StringUtils.isNotBlank(brandNameChn)) {
                        brandNameChnFirstChar = SearchUtils.getFirstChar(brandNameChn);
                    }
                }
            }
        }

        ArrayList<Integer> specificationIdList = new ArrayList<>();
        ArrayList<Integer> specificationParamIdList = new ArrayList<>();
        ArrayList<HashMap<String, Object>> specificationList = new ArrayList<>();

        List<SpecificationParam> specificationParamList = goodsSpecificationRelaMapper.selectBySku(skuId);

        for (SpecificationParam spec : specificationParamList) {
            specificationIdList.add(spec.getSpecificationId());
            specificationParamIdList.add(spec.getSpecificationParamId());

            HashMap<String, Object> specification = new HashMap<>();
            specification.put("specification_id", spec.getSpecificationId());
            specification.put("param_id", spec.getSpecificationParamId());
            specification.put("specification_name", spec.getSpecificationName());
            specification.put("param_name", spec.getSpecificationParam());
            specification.put("catalog_id", thirdCatalogId);
            specificationList.add(specification);
        }

        double salesPrice;
        if (rSalesPrice == null || rSalesPrice.getSalesPrice() == null) {
            salesPrice = 0;
        } else {
            salesPrice = rSalesPrice.getSalesPrice().doubleValue();
        }

        Integer salesVolume = salesVolumeMapper.selectTotalVolumeBySkuId(skuId);

        HashMap<String, Object> goodInfo = new HashMap<>();
        goodInfo.put("sales_price", salesPrice);
        goodInfo.put("pic_path", getPicPath(goodsDesc, goods));
        goodInfo.put("second_catalog_id", secondCatalogId);
        goodInfo.put("second_catalog_name", secondCatalogName);
        goodInfo.put("second_catalog_is_show", secondCatalogIsShow);
        goodInfo.put("third_catalog_id", thirdCatalogId);
        goodInfo.put("third_catalog_name", thirdCatalogName);
        goodInfo.put("third_catalog_parent_id", thirdCatalogParentId);
        goodInfo.put("third_catalog_is_show", thirdCatalogIsShow);

        goodInfo.put("brand_name", brandName);
        goodInfo.put("brand_id", brandId);
        goodInfo.put("brand_name_chn_first_char", brandNameChnFirstChar);
        goodInfo.put("brand_status", brandStatus);

        goodInfo.put("specification_id_list", specificationIdList);
        goodInfo.put("specification_param_id_list", specificationParamIdList);
        goodInfo.put("specification_list_str", JSON.toJSONString(specificationList));
        //goodInfo.put("specification_list_str", "");

        goodInfo.put("sales_volume", salesVolume == null ? 0 : salesVolume);

        return goodInfo;
    }

    /**
     * 获取sku的图片，假如sku的图片为空，取spu的图片
     * @param goodsDesc
     * @param goods
     * @return
     */
    private String getPicPath(RGoodsDesc goodsDesc, RGoods goods) {
        String picPath = "";
        if (goodsDesc == null  || StringUtils.isBlank(goodsDesc.getPicPath())) {
            List<RSpuDescM> spuDescList = spuDescMMapper.selectBySpuId(goods.getSpu())
                .stream().filter(spuDescM -> Integer.valueOf(1).equals(spuDescM.getIsmain())).collect(Collectors.toList());
            if (spuDescList.size() > 0) {
                picPath = spuDescList.get(0).getPicPath();
            }
        } else {
            picPath = goodsDesc.getPicPath();
        }
        return picPath;
    }

    public HashMap<String, Object> getSalesStatus(Integer skuId) {
        int saleStatus;
        int showStatus;
        RSalesStatus rSalesStatus = rSalesStatusDao.getRSalesStatusBySkuId(skuId);

        //        上架状态为空的话就认为商品下架
        saleStatus = Optional.ofNullable(rSalesStatus.getStatus()).orElse(2);
        //        显示状态为空的话就认为商品不显示
        showStatus = Optional.ofNullable(rSalesStatus.getShowStatus()).orElse(0);

        return new HashMap<String, Object>() {
            {
                put("saleStatus", saleStatus);
                put("showStatus", showStatus);
            }
        };
    }
}
